package com.cuihanyu.newuniversityover.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cuihanyu.newuniversityover.R;
import com.cuihanyu.newuniversityover.sql.DBManger;
import com.cuihanyu.newuniversityover.sql.MySQLiteHelper;
import com.cuihanyu.newuniversityover.sql.SQLiteUtil;
import com.cuihanyu.newuniversityover.util.MyLogUtil;

/**
 * Created by cuihanyu on 2018/3/23.
 */

public class NoteBookDetailActivity extends AppCompatActivity implements View.OnClickListener,View.OnTouchListener {
    private MySQLiteHelper helper;
    private SQLiteDatabase db;
    private ImageView mBack, mComplete, mEdit_ImageView;
    private TextView mTitle, mChapter_TextView, mChapter_Small_TV, mNoteBook_TextView, mOk_TextView;
    private EditText mNoteBook_EditText;
    private Intent intent;
    private String mTime;
    private String mChapter_name;
    private String mChapter_small;
    private String mNote;

    private int sx;
    private int sy;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notedetail);
        intent = getIntent();
        mTime = intent.getStringExtra("time");
        mChapter_name = intent.getStringExtra("chapter_name");
        mChapter_small = intent.getStringExtra("chapter_small");
        mNote = intent.getStringExtra("note");
        initView();
    }

    private void initView() {
        mOk_TextView = findViewById(R.id.ok_TextView);
        mOk_TextView.setOnClickListener(this);
        mOk_TextView.setVisibility(View.VISIBLE);
        mBack = findViewById(R.id.back_ImageView);
        mBack.setOnClickListener(this);
        mTitle = findViewById(R.id.title_tv);
        mTitle.setText("笔记");
        mComplete = findViewById(R.id.complete_ImageView);
        mComplete.setOnClickListener(this);
        mNoteBook_EditText = findViewById(R.id.NoteBook_EditText);
        mEdit_ImageView = findViewById(R.id.edit_ImageView);
        mEdit_ImageView.setOnClickListener(this);
        mEdit_ImageView.setOnTouchListener(this);
        mChapter_TextView = findViewById(R.id.Chapter_TextView);
        mChapter_Small_TV = findViewById(R.id.Chapter_Small);
        mNoteBook_TextView = findViewById(R.id.NoteBook_TextView);
        mChapter_TextView.setText(mChapter_name);
        mChapter_Small_TV.setText(mChapter_small);
        mNoteBook_TextView.setText(mNote);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_ImageView:
                finish();
                break;
            case R.id.edit_ImageView:
                mNoteBook_EditText.setVisibility(View.VISIBLE);
                mNoteBook_EditText.setText(mNoteBook_TextView.getText());
                mNoteBook_TextView.setVisibility(View.GONE);
                mEdit_ImageView.setVisibility(View.GONE);
                mComplete.setVisibility(View.VISIBLE);
                mNoteBook_EditText.requestFocus();
                mNoteBook_EditText.setSelection(mNoteBook_EditText.getText().length());
                InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(mNoteBook_EditText, 0);
                break;
            case R.id.complete_ImageView:
                mNoteBook_TextView.setVisibility(View.VISIBLE);
                mNoteBook_TextView.setText(mNoteBook_EditText.getText());
                mNoteBook_EditText.setVisibility(View.GONE);
                mEdit_ImageView.setVisibility(View.VISIBLE);
                mComplete.setVisibility(View.GONE);
                break;
            case R.id.ok_TextView:
                if (mComplete.getVisibility() == View.VISIBLE) {
                    Toast.makeText(this, "您还未修改完成~", Toast.LENGTH_SHORT).show();
                } else if (mNote.equals(mNoteBook_TextView.getText().toString())) {
                    Toast.makeText(this, "您并未做出任何修改~", Toast.LENGTH_SHORT).show();
                } else {
                    helper = DBManger.getIntance(this);
                    db = helper.getWritableDatabase();
                    String sql = "update " + SQLiteUtil.TABLE_NAME + " set " + SQLiteUtil.NOTE + "='" +
                            mNoteBook_TextView.getText().toString() + "' where " + SQLiteUtil.TIME +
                            " = '" + mTime + "'";
                    DBManger.execSQL(db, sql);
                    db.close();
                    Toast.makeText(this, "修改完成!", Toast.LENGTH_SHORT).show();
                    setResult(1);
                    finish();
                }
                break;
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (v.getId()){
            // 如果手指放在imageView上拖动
            case R.id.edit_ImageView: // event.getRawX(); //获取手指第一次接触屏幕在x方向的坐标
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:// 获取手指第一次接触屏幕
                        sx = (int) event.getRawX();
                        sy = (int) event.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:// 手指在屏幕上移动对应的事件
                        int x = (int) event.getRawX();
                        int y = (int) event.getRawY();
                        // 获取手指移动的距离
                        int dx = x - sx;
                        int dy = y - sy;
                        // 得到imageView最开始的各顶点的坐标
                        int l = mEdit_ImageView.getLeft();
                        int r = mEdit_ImageView.getRight();
                        int t = mEdit_ImageView.getTop();
                        int b = mEdit_ImageView.getBottom();
                        // 更改imageView在窗体的位置
                        mEdit_ImageView.layout(l + dx, t + dy, r + dx, b + dy);
                        // 获取移动后的位置
                        sx = (int) event.getRawX();
                        sy = (int) event.getRawY();
                        break;
                    case MotionEvent.ACTION_UP:// 手指离开屏幕对应事件
                        // 记录最后图片在窗体的位置
                        int lasty = mEdit_ImageView.getTop();
                        int lastx = mEdit_ImageView.getLeft();
                        break;
                }
                break;
        }
        return false;
    }
}
